<template>
    <div class="custom-select" :class="{ 'open': isOpen }">
        <div class="select-header" @click="toggleSelect">
            <span class="select-value">{{ selectedOption ? selectedOption.name : placeholder }}</span>
            <span class="select-arrow"></span>
        </div>
        <div class="select-dropdown" v-if="isOpen" :style="{ 'max-height': maxDropdownHeight }">
            <ul class="option-list" ref="optionList">
                <li v-for="(option, index) in options" :key="option.name || index"
                    :class="{ 'selected': selectedOption && option.name === selectedOption.name }"
                    @click="selectOption(option)" class="option-item" ref="optionItems">
                    <!-- 添加默认插槽，允许自定义选项内容 -->
                    <slot :option="option" :index="index">
                        {{ option.label }}
                    </slot>
                </li>
            </ul>
        </div>
    </div>
</template>

<script>
export default {
    name: 'CustomSelect',
    props: {
        options: {
            type: Array,
            default: () => []
        },
        selectedOption: {
            type: Object,
            default: null
        },
        placeholder: {
            type: String,
            default: '请选择'
        }
    },
    data() {
        return {
            isOpen: false,
            optionHeight: 0,
            maxVisibleOptions: 4
        }
    },
    computed: {
        maxDropdownHeight() {
            // 计算最大高度：单个选项高度 * 最大显示数量 + 边框等
            return `${this.optionHeight * this.maxVisibleOptions + 2}px`;
        }
    },
    methods: {
        toggleSelect() {
            this.isOpen = !this.isOpen;
            if (this.isOpen) {
                this.$nextTick(() => {
                    this.calculateOptionHeight();
                });
            }
        },
        selectOption(option) {
            this.$emit('update:selectedOption', option);
            this.isOpen = false;
        },
        calculateOptionHeight() {
            if (this.$refs.optionItems && this.$refs.optionItems.length > 0) {
                // 获取第一个选项的高度作为所有选项的高度
                this.optionHeight = this.$refs.optionItems[0].clientHeight;
            }
        }
    }
}
</script>

<style lang="less" scoped>
.custom-select {
    position: relative;
    width: 100%;

    .select-header {
        display: flex;
        justify-content: space-between;
        align-items: center;
        padding: 20px 22px;
        border: 1px solid #ccc;
        border-radius: 4px;
        cursor: pointer;
        font-size: 1.6vw;

        .select-value {
            flex: 1;
        }

        .select-arrow {
            width: 0;
            height: 0;
            border-left: 5px solid transparent;
            border-right: 5px solid transparent;
            border-top: 5px solid #333;
        }
    }

    .select-dropdown {
        position: absolute;
        top: 100%;
        left: 0;
        right: 0;
        border: 1px solid #ccc;
        border-top: none;
        border-radius: 0 0 4px 4px;
        background-color: white;
        z-index: 1000;
        overflow-y: auto; // 添加滚动条

        .option-list {
            list-style: none;
            padding: 0;
            margin: 0;

            .option-item {
                cursor: pointer;
                padding: 10px 15px; // 确保有内边距

                &:hover {
                    background-color: #f0f0f0;
                }

                &.selected {
                    background-color: #e0e0e0;
                }
            }
        }
    }
}
</style>